Repository navigation
Add WebSocket support to connect-go client and server - #1083
jonbodner-buf wants to merge 34 commits into
Conversation
Signed-off-by: Jon Bodner <jbodner@buf.build>
Signed-off-by: Jon Bodner <jbodner@buf.build>
Signed-off-by: Jon Bodner <jbodner@buf.build>
Signed-off-by: Jon Bodner <jbodner@buf.build> # Conflicts: # handler.go # option.go
… RFC. This is prototype level, but well tested. RFC still needs more human review. Signed-off-by: Jon Bodner <jbodner@buf.build>
Signed-off-by: Jon Bodner <jbodner@buf.build>
Signed-off-by: Jon Bodner <jbodner@buf.build>
Signed-off-by: Jon Bodner <jbodner@buf.build>
Signed-off-by: Jon Bodner <jbodner@buf.build>
Signed-off-by: Jon Bodner <jbodner@buf.build>
…tation to only one byte for the header Signed-off-by: Jon Bodner <jbodner@buf.build>
Signed-off-by: Jon Bodner <jbodner@buf.build>
Signed-off-by: Jon Bodner <jbodner@buf.build>
- Option: new connecthttp.WithWebSocketKeepAlive(interval), backed by wstransport.WithKeepAlive. It applies to both client and server, defaults to 30s, and <= 0 turns it off.
- Pings and timeout: each side Pings on that interval (keepalive.go). A peer that sends nothing for two intervals (no message and no Pong) gets hung up on. The server closes with 1011 and the RPC fails with Canceled on the server. The client closes
with 3111 and the RPC fails with Unavailable on the client. The error messages say it was a keep-alive timeout.
- New frameReader (frame_reader.go): a goroutine that reads one message ahead of whoever consumes it. It's needed because the websocket library only processes control frames during a read, so Pongs would otherwise go unread. It still keeps
backpressure, and it tracks when the peer was last heard from. It stops counting silence while a received message sits untaken, so a slow local consumer isn't blamed on the peer.
- Wiring: both the client (wsClientCall.ensureDialed) and the server (session.Serve) now read through frameReader instead of calling conn.Read directly. On the server, the reader is detached from the RPC deadline, so the library doesn't close the
connection before the deadline's S message can be sent.
- Spec: new §13.2 "Keep-alive" in docs/connect-websocket-protocol.md, plus a row in the close-code table.
2. Peer leaving after the client's C message
- Before: if the client sent C and then disconnected while the handler was still working, nobody noticed.
- Now: the post-C drain goroutine sets leftAfterEndOfStream and cancels the call's context (a new context.WithCancel in session.Serve).
- Close and peerGone: Close skips writing S and calls CloseNow in that case. peerGone() now also checks this flag and asks frameReader whether the connection ended, waiting up to 100ms (peerGoneGrace).
- Deadlines don't count: a read that failed because this end's own deadline expired doesn't mark the peer as gone.
3. Client close code 3111 for local faults
- websocketClientConn records a localFault when Send fails with Internal (a codec problem) or ResourceExhausted (it hit sendMaxBytes).
- CloseResponse then closes with 3111 instead of 1000. A peer fault still takes precedence.
- CloseResponse now treats net.ErrClosed as success, because the background reader may already have finished the close handshake.
- The client's read-error classification moved out into readFailure(). It's the same logic, plus the keep-alive timeout check first.
4. UTF-8 validation on text frames
textEncodingError (message.go) rejects text messages that aren't valid UTF-8 with InvalidArgument / FaultMessageEncoding, on both client and server. Neither the websocket library nor anything under it checks this, but browsers do.
Tests
- New: keepalive_ext_test.go (7 tests: silent peers in each direction, a client that answers Pings is spared, a zero interval disables it, an idle stream stays up, a client that stops receiving gets hung up on, and the connecthttp option reaches both
sides), plus keepalive_test.go, which checks the default is on.
- Added to existing files: client close codes 3111 and 1000, invalid and valid UTF-8, and the connection ending after C cancelling the call. There are also small tweaks in close_test.go and faults_ext_test.go.
- Not run: I haven't run any of these yet. That's worth doing before you commit, given how much the read path changed.
Signed-off-by: Jon Bodner <jbodner@buf.build>
Signed-off-by: Jon Bodner <jbodner@buf.build>
…les specified in the websocket spec. Signed-off-by: Jon Bodner <jbodner@buf.build>
Signed-off-by: Jon Bodner <jbodner@buf.build>
Signed-off-by: Jon Bodner <jbodner@buf.build>
…al messages to send from the client. Signed-off-by: Jon Bodner <jbodner@buf.build>
Signed-off-by: Jon Bodner <jbodner@buf.build>
Signed-off-by: Jon Bodner <jbodner@buf.build>
Signed-off-by: Jon Bodner <jbodner@buf.build>
Signed-off-by: Jon Bodner <jbodner@buf.build>
Signed-off-by: Jon Bodner <jbodner@buf.build>
Signed-off-by: Jon Bodner <jbodner@buf.build>
Signed-off-by: Jon Bodner <jbodner@buf.build>
Signed-off-by: Jon Bodner <jbodner@buf.build>
Signed-off-by: Jon Bodner <jbodner@buf.build>
Signed-off-by: Jon Bodner <jbodner@buf.build>
Signed-off-by: Jon Bodner <jbodner@buf.build>
Signed-off-by: Jon Bodner <jbodner@buf.build>
Signed-off-by: Jon Bodner <jbodner@buf.build>
ab8207d to
d126432
Compare
emcfarlane
left a comment
There was a problem hiding this comment.
I left the issues that matter most. A few are correctness bugs with repros: RPCs over 5s, sticky receive errors and EOF from the handshake.
The bigger concern is size. This is about 6k lines of production code, 10k of tests and 1.8k of examples, with about 28 new exported identifiers. Much of it implements SHOULD and MAY parts of the spec like keep-alive, shutdown, drain and fault hooks. That is hard to review and hard to maintain as the reference implementation. I'd like to land a minimal version first that meets the MUSTs and passes conformance, then add options as users ask.
| return err | ||
| } | ||
| } | ||
| time.Sleep(s.linger) |
There was a problem hiding this comment.
time.Sleep must not be used in tests.
| // session holds no configuration: everything it needs arrives on SessionInfo, | ||
| // so a Session cannot be configured differently from the handshake that | ||
| // produced it. | ||
| type session struct{} |
There was a problem hiding this comment.
An empty struct doesn't make sense to me.
|
|
||
| // Detached from the RPC's deadline: see websocketHandlerConn.Close. Bounded | ||
| // so that an unresponsive peer cannot hold the session open. | ||
| closeCtx, closeCancel := context.WithTimeout(context.WithoutCancel(ctx), wsCloseWriteTimeout) |
There was a problem hiding this comment.
This is a 5s timeout on the start of a connection. It looks like any call past it will fail.
There was a problem hiding this comment.
This is already fixed.
| // through [connect.Server.Call]. Non-RPC requests get a plain 404, and | ||
| // requests for unknown services fall through to mux. | ||
| // | ||
| // Every route also serves Connect over WebSocket unless [WithoutWebSocket] is |
There was a problem hiding this comment.
Serving WebSocket on every route by default is a big change for existing users. Request headers arrive in M after the handshake, so HTTP middleware that authenticates or trusts proxy headers never sees them. A deny list only covers the headers we know about. Can the server opt in instead?
| // client that consistently sends unknown markers from one that mismatches a | ||
| // frame type once. The Connect code alone cannot: every framing fault is | ||
| // Internal, separable only by matching message strings. | ||
| type ProtocolFault = wstransport.ProtocolFault |
There was a problem hiding this comment.
This adds about 28 exported identifiers. Most are tuning or observability: fault types, protocol error handlers, a logger, keep-alive, handshake and max timeouts, compression, selectors. None can be removed after release. I'd start with one option to enable WebSocket and add the rest when users ask. WithConditionalOptions already covers per-procedure selection.
| return | ||
| } | ||
| } | ||
| k.pings.Go(func() { k.ping(ctx) }) |
There was a problem hiding this comment.
This starts a goroutine per tick and each one waits up to the keep-alive timeout. When pongs aren't read because read-ahead is full, up to 11 pile up per RPC. Server pings are a SHOULD and the idle timeout a MAY in the spec. Can this wait for a follow-up?
| t.mu.Lock() | ||
| if _, registered := t.registered[server]; !registered { | ||
| t.registered[server] = struct{}{} | ||
| server.RegisterOnShutdown(func() { t.shutdown(server) }) |
There was a problem hiding this comment.
Shutdown tracking is a SHOULD in the spec and takes a handler-wide lock twice per RPC. It also misses cases. A session that starts after the hook fires registers a hook that never runs, because shutdown deletes the server. http.Server.Close runs no hooks. Shutdown can return before any S is written. Clients already see unavailable when the connection drops, so I'd leave this out.
| defer wire.endMessage() | ||
| // One byte past the limit is all it takes to know the message overran, and | ||
| // reading more is what the limit exists to prevent. | ||
| var message bytes.Buffer |
There was a problem hiding this comment.
A new bytes.Buffer per message that grows in 512 byte steps costs about 1.5 KB and 2 allocs even for a tiny message. It is most of the B/op in a bidi round trip. conn.Read sizes from the frame.
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| // Package connectwire implements the Connect protocol's JSON error |
There was a problem hiding this comment.
connectwire and connecterr move code out of connecthttp behind aliases only so wstransport can use it. Keeping the WebSocket code in connecthttp would avoid this churn in the HTTP protocols.
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| // Command server serves the ping service over two transports at once: unary |
There was a problem hiding this comment.
Can the examples move to a follow-up? They add 1.8k lines that mostly repeat test coverage.
…cts. Signed-off-by: Jon Bodner <jbodner@buf.build>
Signed-off-by: Jon Bodner <jbodner@buf.build>
|
Closing in favour of #1096 |
This PR adds WebSocket support to connect-go's client and server, meeting the spec that's being added in connectrpc/connectrpc.com#452
This PR needs to merge before the WebSocket conformance tests in connectrpc/conformance#1200 because connect-go is the reference implementation for conformance tests. Currently, the
go.modin that PR is referring to a branch instead of main and that needs to be updated before the tests can be merged.